/*
 * money.js
 *
 * Copyright (C) 2006 - OS3 srl - http://www.os3.it
 *
 * Written by: Fabio Rotondo - fabio.rotondo@os3.it
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation;
 * version 2 of the License ONLY.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * NOTE: this is the GPL version of the library. If you want to include this 
 *       library inside a CLOSED SOURCE / PROPRIETARY software, you need 
 *       to purchase a COMMERCIAL LICENSE. See http://www.os3.it/license/
 *       for more info.
 */
// PUBLIC: money_format
function money_format ( v )
{
	var m = new Money ( v );

	return m.value;
}

// PUBLIC:
//		Money
//		value
//		set
//		add
function Money ( v )
{
	this.value = '';

	this.set  = money_meth_set;
	this.add  = money_meth_add;

	this._format   = money_int_format;
	this._to_money = money_int_float_to_money;

	if ( v ) this.set ( v );
}

// PUBLIC: fromLongInt
Money.fromLongInt = function ( v )
{ 
        var s = new String ( v );

        if ( s.indexOf ( "," ) == -1 ) 
	{
		if ( s.length > 2 ) 
        	{
                	var e = s.substr ( s.length-2, 2 );
                	s = s.substr ( 0, s.length -2 );

                	s += "," + e;
		} else 
			s = "0," + s;
        }

	var m = new Money ( s );

        return m.value;
};

Money.toFloat = function ( amount )
{
	var sep;
	var str;
	var start;

	amount = new String ( amount );

	if ( amount == "" ) return 0.0;

	amount = amount.replace ( /\./g, '' );

	sep = amount.indexOf ( ',' );

	if ( sep == -1 ) 
		amount += ".00";
	else 
	{
        	start = amount.substring ( 0, sep );
            	str = amount.substring ( sep + 1 );

		amount = start + "." + str;
	}

	return parseFloat ( amount );
};

Money.toMoney = function ( amount )
{
	var m = new Money ( amount );

	return m.value;
};


function money_meth_set ( v )
{
	this.value = this._format ( v );
	
	return this.value;
}

function money_meth_add ( v )
{
	var f1 = Money.toFloat ( this.value );
	var f2 = Money.toFloat ( v );

	return this._to_money ( f1 + f2 );
}

function money_int_format ( amount ) 
{
	var out;
  	var cent;
  	var sign = "";
	var i;

	if ( typeof amount == 'number' ) 
	{
		this._to_money ( amount );
		return this.value;
	} 

	// console.debug ( "Money: amt1: "+ amount );
	amount = new String ( amount );

	// console.debug ( "Money: amt2: "+ amount );

	amount = amount.strip ();
	if ( ! amount.length ) return '';

	// remove the "-" (or "+") sign
	amount = amount.replace ( /[+-]/g, "" );

	if ( amount.indexOf ( ',' ) == -1 ) amount += ",";
	amount += "00";

	cent   = amount.replace ( new RegExp ( ".*,(..).*" ),"$1" );
	amount = amount.replace ( new RegExp ( "(.*),.*" ),"$1" );
	amount = amount.replace ( new RegExp ( "\\.", "g" ), '' );

	// console.debug ( "Money: amt4: "+ amount );

    	for ( i = 0; i < Math.floor ( ( amount.length - ( 1 + i ) ) / 3 ); i++ ) 
      		amount = amount.substring(0,amount.length-(4*i+3))+'.'+amount.substring(amount.length-(4*i+3));

    	if ( amount == "" ) amount = "0";
	if ( cent.length == 1 ) cent = "0" + cent;

    	out = ( sign + amount + ',' + cent );

	return out;
}

function money_int_float_to_money ( amount )
{
        var s = new String ( amount );

        // Replace the "." with ","
	s = s.replace ( /\./g, "," );

	this.value = this._format ( s );

	return this.value;
}
